/////////////////////////////////////////////
/// Implementation of the simple template "Tell me * of *"
/////////////////////////////////////////////

// fetching the log
log = context.getLog()

context.startComposing()
wildcard1 = context.getMatch(1);

//sparqlquery
documentQuery = "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> "+
"SELECT DISTINCT ?concept "+
"WHERE { "+
"?subject a ?tmpcon . "+
"?subject rdfs:label ?tmpsub. "+
"FILTER bif:contains(?tmpsub,\""+wildcard1+"\"@en ). "+
"?tmpcon rdfs:label ?concept. }"+
"Limit 50"

toUser = "a/an "+wildcard1+" might be one of the following: \n"
urlglobal = "http://dbpedia.openlinksw.com:8890/sparql?default-graph-uri=http://dbpedia.org"
//urllocal = "http://139.18.2.37:8890/sparql?default-graph-uri=dbpedia.org"
url = urlglobal
errors = ""
context.queryRemote(documentQuery,url).each{table ->
	if(table.errors!=null){
		errors += table.errors + " by " + table.storeName
	}else{
		table.resultRows.each{row ->
			toUser += row["concept"] + ", "
		}
	}
}
context.stopComposing()
context.sendMessageToUser(toUser)
if(errors.length()>0){
	context.sendMessageToUser("Unfortunately some errors occured : " + errors)
	context.sendMessageToUser("It can be possible that you retrieved a time out, because DBpedia needed a long time. You could try the same query again, in the hope that DBpedia cached your query." )
}


